var fs = require('fs');
var path = require('path');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
module.exports = function(app,co){
	app.use('/upload/location',multipartMiddleware,function(req,res){
		// elementUI的问题，虽然一次性可以上传多张图，但是发了多个请求。这里需要注意
		var file = req.files.file;
		var readFrom = fs.createReadStream(file.path);
		var fileName = path.basename(file.path);
		var originalName = file.originalFilename;
		var timeStamp = Math.round(new Date().getTime()/1000);
		var saveTo = fs.createWriteStream(conf.upload.locationPath+'/'+timeStamp+originalName);
		readFrom.pipe(saveTo);
		saveTo.on('finish',function(){
			fs.unlinkSync(file.path);
		})
		res.json({
			code:200,
			name:timeStamp+originalName
		})
	})
}